package com.dev;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.text.SpannableStringBuilder;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.fb.FileBrowserPage;
public class JavaEditorTab extends TabBar.TypicTab {
	public JavaEditorPage jep;
	public TabData.JavaEditorInfo jei;
	public boolean canSave;
	public boolean memory=true;
	static int textColorDefault=0xffff00ff;
    public JavaEditorTab(MainActivity ma) {
		super(ma);
		setTextColor(textColorDefault);
	}
	@Override
	public boolean isNeedRemember() {
		return memory;
	}
	static class Menu extends LinearLayout implements OnClickListener {
		WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
		JavaEditorTab jet;
		int borderWidth=2;
		FileBrowserTabMenu.Item i0,i1,i2,i3,i4,i5,i6,i7;
		CheckableItem ci1,ci2,ci3,ci4,ci5,ci6,ci7,ci8;
		LayoutParams itlp;
		MainActivity ma;
		Menu(MainActivity ma) {
			super(ma);
			this.ma = ma;
			setOrientation(VERTICAL);
			setBackground(new FileBrowserTabMenu.BorderDrawable());
			wlp.gravity = Gravity.LEFT | Gravity.TOP;
			wlp.type = wlp.TYPE_APPLICATION;
			wlp.format = PixelFormat.TRANSPARENT;
			wlp.flags = wlp.FLAG_LAYOUT_IN_SCREEN;
			i0 = new FileBrowserTabMenu.Item(ma, "克隆标签页");
			i1 = new FileBrowserTabMenu.Item(ma, "详情");
			i2=new FileBrowserTabMenu.Item(ma,"格式化代码");
			i2.setOnClickListener(this);
			ci1=new CheckableItem(ma,"显示空格符");
			ci1.setOnClickListener(this);
			ci2=new CheckableItem(ma,"显示制表符");
			i0.setOnClickListener(this);
			i1.setOnClickListener(this);
			itlp = new LayoutParams(-1, (int)(30 * ma.dp));
			addView(i0, itlp);
			addView(i1, itlp);
			addView(i2,itlp);
			addView(ci1,itlp);
		}
		void show(JavaEditorTab jet,View anchor) {
			this.jet = jet;
			Rect rect=new Rect();
			anchor.getGlobalVisibleRect(rect);
			wlp.width = ma.gui.getWidth() * 2 / 3;
			wlp.height = -2;
			wlp.x = rect.left;
			wlp.y = rect.bottom;
			ma.wm.addView(this, wlp);
		}
		void dismiss() {
			ma.wm.removeView(this);
		}
		Point touchDown=new Point();
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			int x=(int)event.getX();
			int y=(int)event.getY();
			switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					touchDown.x = x;
					touchDown.y = y;
					break;
				case MotionEvent.ACTION_UP:
					Rect visible=new Rect();
					getGlobalVisibleRect(visible);
					int[] is=new int[2];
					getLocationOnScreen(is);
					if (touchDown.x >= visible.right || touchDown.x < visible.right || touchDown.y >= visible.bottom || touchDown.y < visible.top) {
						dismiss();
					}
					/*if(touchDown.x>is[0]&&touchDown.x<is[0]+getWidth()&&touchDown.y>is[1]&&touchDown.y<is[1]+getHeight()){
					 }else{
					 dismiss();
					 }*/
			}
			super.onTouchEvent(event);
			return true;
		}
		@Override
		public void onClick(View view) {
			if (view == i0) {//onClick
				JavaEditorTab jet=new JavaEditorTab(ma);
				TabData.JavaEditorInfo jei=new TabData.JavaEditorInfo();
				jei.src = this.jet.jei.src;
				jet.jei = jei;
				jet.setText(jei.src.getName());
				jet.setOnClickListener(ma.gui.TabOnClick);
				for (int index=0,c=ma.gui.tabs.getChildCount();true;index++) {
					if (index < c) {
						View child=ma.gui.tabs.getChildAt(index);
						if (child == this.jet) {
							ma.gui.tabs.addView(jet, index + 1, TabBar.tablp);
							break;
						}
					} else {
						break;
					}
				}
				dismiss();
			} else if (view == i1) {//onClick
				if (codeDetails == null)codeDetails = new CodeDetails(ma);
				codeDetails.show(jet);
				dismiss();
			}else if(view==i2){//onClick
				/*char[] text=jet.jie.text;
				int length=jet.jie.text_length;
				StringBuilder sb=new StringBuilder();
				outmost:
				for(int index=0;true;index++){
					if(index<length){
						char ch=text[index];
					}else{
						break;
					}
				}*/
			}else if(view==ci1){//onClick
				if(ci1.isSelected()){
					ci1.setSelected(false);
					jet.jep.jie.showSpaceChar=false;
					jet.jep.jie.invalidate();
				}else{
					ci1.setSelected(true);
					jet.jep.jie.showSpaceChar=true;
					jet.jep.jie.invalidate();
				}
			}
		}
	}
	static CodeDetails codeDetails;
	static class CodeDetails extends LinearLayout implements OnClickListener {
		WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
		JavaEditorTab jet;
		int borderWidth=2;
		FileBrowserTabMenu.Item i0,i1,i2,i3,i4,i5,i6,i7;
		CheckableItem ci1,ci2,ci3,ci4,ci5,ci6,ci7,ci8;
		LayoutParams itlp;
		MainActivity ma;
		LinearLayout titlebar;
		TextView title,msg;
		IconViewClose close;
		CodeDetails(MainActivity ma) {
			super(ma);
			this.ma = ma;
			setOrientation(VERTICAL);
			setBackground(new GUI.BorderDrawable());
			title = new TextView(ma);
			title.setTextColor(0xff000000);
			title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
			title.setGravity(Gravity.CENTER_VERTICAL);
			title.setPadding(9, 0, 0, 0);
			title.setText("代码信息");
			close = new IconViewClose(ma);
			close.setOnClickListener(this);
			titlebar = new LinearLayout(ma){
				public void onDraw(Canvas cvs) {
					super.onDraw(cvs);
					Paint p=new Paint();
					p.setColor(MainActivity.CYAN);
					cvs.drawRect(0, getHeight() - FileBrowserPage.borderWidth, getWidth(), getHeight(), p);
				}
			};
			titlebar.setWillNotDraw(false);
			titlebar.setOrientation(HORIZONTAL);
			titlebar.addView(title, new LayoutParams(0, -1, 1));
			titlebar.addView(close);
			msg = new TextView(ma);
			msg.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 13);
			msg.setTypeface(Typeface.MONOSPACE);
			msg.setTextColor(0xff000000);
			msg.setPadding(10, 0, 0, 0);
			msg.setText("文件尚未保存，是否立即保存？");
			addView(titlebar,-1,(int)(30*ma.dp));
			addView(msg,-1,-2);
		}
		void show(JavaEditorTab jet) {
			this.jet = jet;
			JavaIndentEditor jie=jet.jep.jie;
			wlp.width = ma.wp * 2 / 3;
			wlp.height = -2;
			long length=jet.jei.src.length();
			String infoPath="路径："+jet.jei.src.getAbsolutePath();
			String infoSize="大小："+length;
			String infoLineCount="行数：";
			SpannableStringBuilder ssb=new SpannableStringBuilder();
			if(length>1024){
				infoSize+="("+length/1024+")";
			}
			if(jie==null){
				ssb.append(infoPath+'\n');
				ssb.append(infoSize+'\n');
			}else{
				StringBuilder sb=jie.text;
				int len=sb.length();
				int lc=1;
				for(int index=0;index<len;index++){
					char ch=sb.charAt(index);
					if(ch=='\n'){
						lc++;
					}
				}
				infoLineCount+=lc;
				ssb.append(infoPath+'\n');
				ssb.append(infoSize+'\n');
				ssb.append(infoLineCount+'\n');
			}
			msg.setText(ssb);
			ma.wm.addView(this, wlp);
		}
		void dismiss() {
			ma.wm.removeView(this);
		}
		@Override
		public void onClick(View view) {
			if (view == close) {
				dismiss();
			}
		}
	}
}
